package com.haker.mensa2;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;   
    
    
   public class WGet {
    	
    	private static final int HTTP_STATUS_OK = 200;

        private static byte[] sBuffer = new byte[512];
        
        private static String sUserAgent = null;
    
        public static class AException extends Exception {
            public AException(String detailMessage, Throwable throwable) {
                super(detailMessage, throwable);
            }

            public AException(String detailMessage) {
                super(detailMessage);
            }
        }
        
        public static void prepareUserAgent(Context context) {
            try {
                // Read package name and version number from manifest
                PackageManager manager = context.getPackageManager();
                PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
                sUserAgent = String.format("%s/%s (Linux; Android)",
                        info.packageName, info.versionName);

            } catch(NameNotFoundException e) {
                Log.e("WGet", "Couldn't find package information in PackageManager", e);
            }	
        }
        
        protected static String getUrlContent(String url) throws AException {
    	;
    
    	
    	if (sUserAgent == null) {
            throw new AException("User-Agent string must be prepared");
        }

        // Create client and set our specific user-agent string
    	 System.out.println(url);
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
       
        request.setHeader("User-Agent", sUserAgent);

        try {
        	 HttpResponse response = client.execute(request);
        	 System.out.println(url);
        	  // Check if server response is valid
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != HTTP_STATUS_OK) {
                throw new AException("Invalid response from server: " +
                        status.toString());
            }

            // Pull content stream from response
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();

            ByteArrayOutputStream content = new ByteArrayOutputStream();

            // Read response into a buffered stream
            int readBytes = 0;
            while ((readBytes = inputStream.read(sBuffer)) != -1) {
                content.write(sBuffer, 0, readBytes); 
            }

            // Return result from buffered stream
            return  new String(content.toByteArray());
        } catch (IOException e) {
            throw new AException("Problem communicating with API", e);
        }
    }
   }
    
   
